<?php
class Host extends CI_Controller{
	function __construct(){
		parent::__construct();
		//判断session
		if (!$this->session->userdata('name')){
			header("Location: /host");
		}
	}
	
	function index(){
		$this->load->model('consumable_model');
		//$cat = $this->input->get('cat');
		$key = $this->input->get('key');
		$options = $this->input->get('options')?$this->input->get('options'):'0';
		$from = $this->input->get('from');
		$to = $this->input->get('to');
		$offset = $this->input->get('offset');
		$where = array(
			//'cat' => $cat,
			'key' => $key,
			'options' => $options,
			'from' => $from,
			'to' => $to,
			'cid' => '1',
		);
		
		$this->load->library('pagination');
		$page_config = render_pagination_config_array();	
		$page_config['base_url'] = '/host/?key='.$key.'&options='.$options.'&from='.$from.'&to='.$to;
		$page_config['total_rows'] = $this->consumable_model->get_all_rows($where);
		$this->pagination->initialize($page_config);
		$pager_link	= $this->pagination->create_links();
		
		$staff_list = $this->consumable_model->get_consumable_by_page($page_config['per_page'],$offset,$where);
		
		$data['sidebar'] = array('office','host');
		$data['query'] = $staff_list;
		$data['page'] = $pager_link;
		$data['options'] = $options;
		$data['key'] = $key;
		$data['cid'] = '1';
		$data['search'] = array(
			'key' => $key,
			'from' => $from,
			'to' => $to,
		);

		load_view('host',$data);
	}
	
	function new_host(){
		$data['sidebar'] = array('office','host/new_host');
		$data['query'] = '';
		load_view('host_file',$data);
	}
	
	function modify_host($id,$query=''){	
		$this->load->model('consumable_model');
		$obj = new Consumable_model($id);
		$data['sidebar'] = array('office','host');
		$data['query'] = array(
			'id' => $id,
			'name' => $obj->name,
			'brand' => $obj->brand,
			'price' => $obj->price,
			'condition' => $obj->condition,
			'bought' => $obj->bought,
			'remark' => $obj->remark,
			'host_parts' => parts_list($id),
			'query' => $query,
		);
		load_view('host_file',$data);
	}
	
	function execute(){
		$query = $this->input->post('query');
		$id = $this->input->post('id');
		$name = trim($this->input->post('name'));
		$brand = trim($this->input->post('brand'));
		$price = trim($this->input->post('price'));
		$condition = trim($this->input->post('condition'));
		$bought = strtotime($this->input->post('bought'));
		$remark = $this->input->post('remark');
		$categoryid = '1';
		$count = $this->input->post('count');
	
		$this->load->model('consumable_model');
		$obj = new Consumable_model($id);
		$obj->name = $name;
		$obj->brand = $brand;
		$obj->price = $price;
		$obj->condition = $condition;
		$obj->bought = $bought;
		$obj->remark = $remark;
		$obj->categoryid = $categoryid;

		
		if($id){
			$obj->changed = time();
			$obj->update();
			l('2',$name,$obj->sid,$id,'1');
		}else{
			$obj->created = $obj->changed = time();
			$obj->status = '0';
			for($i=0;$i<$count;$i++){
				$GLOBALS['table'] = 'consumable';//避免数据表被logs替换
				l('1',$name,'0',$obj->save(),'1');
			}		
		}
		$url = $query?'?'.$query:'';
		echo "<script language=javascript>
				window.location='".base_url()."host".$url."';
				</script>";
	}
	
	function del_host($id){
		$this->load->model('consumable_model');
		$obj = new Consumable_model($id);
		$obj->delete();
		l('3',$obj->name,$obj->sid,$id,'1');
		echo "<script>location.replace(document.referrer);</script>";
	}
	
	function install(){
		$sid = $this->input->post('sid');
		$id = $this->input->post('id');
		if($sid != '0'){
			$this->load->model('consumable_model');
			$obj = new Consumable_model($id);
			$obj->sid = $sid;
			$obj->status = '1';
			$obj->update();
			$staff = staff_options();
			$key = $obj->name."到员工".$staff[$sid];
			l('4',$key,$sid,$id,'1');
		}
		echo "<script>location.replace(document.referrer);</script>";
	}
	
	function unstall(){
		$id = $this->input->post('id');
		$status = $this->input->post('status');
		$this->load->model('consumable_model');
		$obj = new Consumable_model($id);
		$sid = $obj->sid;
		$obj->sid = '0';
		$obj->hid = '0';
		$obj->status = $status=='5'?'0':'2';
		$obj->update();
		$staff = staff_options();
		$key = $obj->name."来自于员工".$staff[$sid];
		$obj->unstall_host_parts($id);
		l($status,$key,$sid,$id,'1');
		echo "<script>location.replace(document.referrer);</script>";
	}
	
	function ajax(){
		$data = $this->input->post('data');
		$data = explode('_',$data);
		$oid = $data[0];//操作id
		$cid = $data[1];//消耗品id
		
		$this->load->model('consumable_model');
		$obj = new Consumable_model($cid);
		$sid = $obj->sid;
		$hid = $obj->hid;
		$obj->sid = '0';
		$obj->hid = '0';
		$obj->status = $oid=='5'?'0':'2';
		$obj->update();
		
		$staff = staff_options();
		$host = host();
		$staff = $staff[$sid]?'属于员工'.$staff[$sid]:'此时未指定员工';
		$key = $obj->name.'-'.$obj->brand.'-'.$obj->condition."从主机".$host[$hid].$staff;
		l($oid,$key,$sid,$cid,'0');
		echo 'success';
	}
	
	function view(){
		$this->load->model('consumable_model');
		//$cat = $this->input->get('cat');
		$key = $this->input->get('key');
		$options = $this->input->get('options')?$this->input->get('options'):'0';
		$from = $this->input->get('from');
		$to = $this->input->get('to');
		$offset = $this->input->get('offset');
		$where = array(
			//'cat' => $cat,
			'key' => $key,
			'options' => $options,
			'from' => $from,
			'to' => $to,
			'cid' => '1',
		);
		
		$this->load->library('pagination');
		$page_config = render_pagination_config_array();	
		$page_config['base_url'] = '/host/view/?key='.$key.'&options='.$options.'&from='.$from.'&to='.$to;
		$page_config['total_rows'] = $this->consumable_model->get_view_rows($where);
		$this->pagination->initialize($page_config);
		$pager_link	= $this->pagination->create_links();
		
		$host_list = $this->consumable_model->get_view($page_config['per_page'],$offset,$where);
		//echo "<pre>";
		//print_r($host_list);exit();
		$data['sidebar'] = array('view','host/view');
		$data['query'] = $host_list;
		$data['page'] = $pager_link;
		$data['options'] = $options;
		$data['key'] = $key;
		$data['cid'] = '1';
		$data['search'] = array(
			'key' => $key,
			'from' => $from,
			'to' => $to,
		);

		load_view('host_view',$data);
	}
	
	function host_detail($hid){
		$this->load->model('consumable_model');
		$detail = $this->consumable_model->parts_detail($hid);//print_r($detail);exit();
		echo json_encode($detail);
	}
}